package com.ykht.proj.coupon.controller.c;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DistributionTypeEnum;
import com.ykht.proj.coupon.Enum.LookRoleTypeEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.FxConfRewardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: lazyBoy
 * @Date: 2021/2/24 13:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/c/fxConfPoster")
@Api(value = "/c/fxConfPoster", tags = "推广海报")
public class ConfPosterController {

	private final FxForwardService fxForwardService;

	private final CnUserExtService cnUserExtService;

	private final FxConfRewardService fxConfRewardService;

	private final FxWaiterExtService fxWaiterExtService;

	private final FxRewardService fxRewardService;

	private final FxConfRewardBaseGlobelService fxConfRewardBaseGlobelService;

	private final FxConfRewardBaseService fxConfRewardBaseService;


	@ApiOperation(value = "c跳转到c端根据服务员id分享、赚取佣金", notes = "分享、赚取佣金")
	@PostMapping("/addForwardRecord")
	public R addForwardRecord(@RequestBody FxForward fxForward) {
		try {
			//根据登录人获取服务员id
			FxWaiterExt waiterExt = fxWaiterExtService.getOne(Wrappers.lambdaQuery(FxWaiterExt.class)
					.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId())
					.eq(FxWaiterExt::getWorkState, 1)
			);
			//如果平台分销总开关关了、不给钱
			FxConfRewardBaseGlobel fxConfRewardBaseGlobel = fxConfRewardBaseGlobelService.getOne(Wrappers.lambdaQuery(FxConfRewardBaseGlobel.class)
					.eq(FxConfRewardBaseGlobel::getDelFlag, 0)
					.eq(FxConfRewardBaseGlobel::getSwitchs, 0)
			);
			if (fxConfRewardBaseGlobel == null) {
				return R.failed("平台分销任务开关未开启，无佣金");
			}
			//如果品牌的分销开关关了、不给钱
			FxConfRewardBase fxConfRewardBase = fxConfRewardBaseService.getOne(Wrappers.lambdaQuery(FxConfRewardBase.class)
					.eq(FxConfRewardBase::getBrandId, waiterExt.getBrandId())
					.eq(FxConfRewardBase::getDelFlag, 0)
					.eq(FxConfRewardBase::getSwitchs, 0)
			);
			fxForward.setCreateUser(SecurityUtils.getUserId());
			fxForward.setCreateTime(LocalDateTime.now());
			fxForward.setWaiterId(waiterExt.getId());
			//保存一条转发记录
			fxForwardService.save(fxForward);
			/*获取登录人门店以及品牌id*/
			/*根据登录人去获取对应门店的转发配置佣金*/
			/*创建奖励记录对象*/
			FxReward fxReward = new FxReward();
			fxReward.setBrandId(waiterExt.getBrandId());
			fxReward.setShopId(waiterExt.getShopId());
			fxReward.setWaiterId(waiterExt.getId());
			fxReward.setCreateUser(SecurityUtils.getUserId());
			fxReward.setCreateTime(LocalDateTime.now());
			fxReward.setCompanyId(waiterExt.getCompanyId());
			fxReward.setType(fxForward.getTaskType());
			fxReward.setTatId(fxForward.getId());
			FxConfRewardVO confReward = new FxConfRewardVO();
			if (fxConfRewardBase != null) {
				if (fxForward.getTaskType().equals(1)) { //如果是转发
					confReward = fxConfRewardService.findConfByBrandIdAndShopId(waiterExt, DistributionTypeEnum.DISTRIBUTION_TYPE_1.getValue());
					fxReward.setType(DistributionTypeEnum.DISTRIBUTION_TYPE_1.getValue());
					if (confReward != null) {
						int i = fxConfRewardBaseGlobel.getForwardArticleMoney().compareTo(confReward.getBaseMoney());
						if (i > 0) {
							fxReward.setMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
							fxForward.setForwardMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
						} else {
							fxReward.setMoney(confReward.getBaseMoney());
							fxForward.setForwardMoney(confReward.getBaseMoney());
						}
					}
				}
			}
			//选择平台基本配置
			else {
				fxConfRewardBaseGlobel = fxConfRewardBaseGlobelService.queryValue();
				if (fxConfRewardBaseGlobel != null) {
					if (fxForward.getTaskType().equals(0)) {
						fxReward.setMoney(fxConfRewardBaseGlobel.getInviteMoney());
						fxForward.setForwardMoney(fxConfRewardBaseGlobel.getInviteMoney());
					}
					if (fxForward.getTaskType().equals(1)) {
						fxReward.setMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
						fxForward.setForwardMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
					}
				} else {
					/*如果平台也没有基本配置，那就不给钱*/
					return R.ok();
				}
			}
			fxReward.setTatId(fxForward.getId());
			fxRewardService.insert(fxReward);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
		return R.ok();
	}
}
